package com.ruoyi.device.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.device.mapper.SysAppUserMapper;
import com.ruoyi.device.domain.SysAppUser;
import com.ruoyi.device.service.ISysAppUserService;

/**
 * 小程序注册Service业务层处理
 * 
 * @author zz
 * @date 2024-04-19
 */
@Service
public class SysAppUserServiceImpl implements ISysAppUserService 
{
    @Value("${wechart.userRole}")
    private String userRoleId;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private SysAppUserMapper sysAppUserMapper;

    /**
     * 查询小程序注册
     * 
     * @param regId 小程序注册主键
     * @return 小程序注册
     */
    @Override
    public SysAppUser selectSysAppUserByRegId(Long regId)
    {
        SysAppUser sysAppUser = sysAppUserMapper.selectSysAppUserByRegId(regId);
        SysDept sysDept = sysDeptService.selectDeptById(sysAppUser.getRegDeptId());
        if(sysDept != null){
            sysAppUser.setRegDept(sysDept.getDeptName());
        }
        return sysAppUser;
    }

    @Override
    public SysAppUser getUserByName(String userName) {
        SysAppUser sysAppUser = new SysAppUser();
        sysAppUser.setRegName(userName);

        List<SysAppUser> sysAppUserList = sysAppUserMapper.selectSysAppUserList(sysAppUser);
        if(sysAppUserList != null && sysAppUserList.size() > 0){
            return sysAppUserList.get(0);
        }
        return sysAppUser;
    }

    @Override
    public SysAppUser getUserByOpenid(String openid) {
        return sysAppUserMapper.selectUserByOpenid(openid);
    }

    @Override
    public List<SysAppUser> selectSysAppUserRegCodeList(String regCode) {
        List<SysAppUser> sysAppUserList = sysAppUserMapper.selectSysAppUserRegCodeList(regCode);
        sysAppUserList.stream().map(sysAppUser -> {
            SysDept sysDept = sysDeptService.selectDeptById(sysAppUser.getRegDeptId());
            if(sysDept != null){
                sysAppUser.setRegDept(sysDept.getDeptName());
            }
            return sysAppUser;
        });
        return sysAppUserList;
    }

    /**
     * 查询小程序注册列表
     * 
     * @param sysAppUser 小程序注册
     * @return 小程序注册
     */
    @Override
    public List<SysAppUser> selectSysAppUserList(SysAppUser sysAppUser)
    {
        List<SysAppUser> sysAppUserList = sysAppUserMapper.selectSysAppUserList(sysAppUser);
        sysAppUserList.stream().map(appUser -> {
            SysDept sysDept = sysDeptService.selectDeptById(appUser.getRegDeptId());
            if(sysDept != null){
                sysAppUser.setRegDept(sysDept.getDeptName());
            }
            appUser.setRegDept(sysDept.getDeptName());
            return appUser;
        });
        return sysAppUserList;
    }

    /**
     * 新增小程序注册
     * 
     * @param sysAppUser 小程序注册
     * @return 结果
     */
    @Override
    public int insertSysAppUser(SysAppUser sysAppUser)
    {
        sysAppUser.setCreateTime(DateUtils.getNowDate());
        sysAppUser.setCreateBy(sysAppUser.getRegName());
        //新增时状态为待审核
        sysAppUser.setRegStatus(0);
        return sysAppUserMapper.insertSysAppUser(sysAppUser);
    }

    /**
     * 修改小程序注册
     * 
     * @param sysAppUser 小程序注册
     * @return 结果
     */
    @Override
    public int updateSysAppUser(SysAppUser sysAppUser)
    {
        //说明审核通过
        if(sysAppUser.getRegStatus() == 1){
            SysUser user = new SysUser();
            user.setRoleId(Long.valueOf(userRoleId));
            user.setUserName(sysAppUser.getRegCode());
            user.setNickName(sysAppUser.getRegName());
            user.setDeptId(sysAppUser.getRegDeptId());
            user.setPhonenumber(sysAppUser.getRegMobile());
            user.setPassword(sysAppUser.getRegPassword());
            user.setWxOpenid(sysAppUser.getRegOpenid());
            sysUserService.insertUser(user);
        }
        SysDept sysDept = sysDeptService.selectDeptById(sysAppUser.getRegDeptId());
        if(sysDept != null){
            sysAppUser.setRegDept(sysDept.getDeptName());
        }
        sysAppUser.setUpdateTime(DateUtils.getNowDate());
        return sysAppUserMapper.updateSysAppUser(sysAppUser);
    }

    /**
     * 批量删除小程序注册
     * 
     * @param regIds 需要删除的小程序注册主键
     * @return 结果
     */
    @Override
    public int deleteSysAppUserByRegIds(Long[] regIds)
    {
        return sysAppUserMapper.deleteSysAppUserByRegIds(regIds);
    }

    /**
     * 删除小程序注册信息
     * 
     * @param regId 小程序注册主键
     * @return 结果
     */
    @Override
    public int deleteSysAppUserByRegId(Long regId)
    {
        return sysAppUserMapper.deleteSysAppUserByRegId(regId);
    }
}
